package views;

import java.util.List;

import models.Song;

import phong.musicplayer.R;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

public class SongAdapter extends ArrayAdapter<Song> {

	Context context;
	int resID;
	List<Song> songs;
	int itemSelected;
	Handler handler;

	public static int ON_CLICK_DOWNLOAD = 10002;

	public SongAdapter(Context context, int resID, List<Song> songs,
			Handler handler) {
		super(context, resID, songs);
		this.context = context;
		this.resID = resID;
		this.songs = songs;
		this.handler = handler;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View v = null;

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		v = inflater.inflate(resID, null);
		final Song song = songs.get(position);

		TextView songTitle = (TextView) v.findViewById(R.id.song_title);
		TextView singer = (TextView) v.findViewById(R.id.singer);

		songTitle.setText(song.getSongTitle());
		singer.setText(song.getSinger());

		Button btnDownload = (Button) v.findViewById(R.id.btnDownload);
		btnDownload.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Message mes = new Message();
				Bundle bundle = new Bundle();
				bundle.putString("song", songs.get(position).getUrlDownload());
				mes.setData(bundle);
				mes.what = ON_CLICK_DOWNLOAD;
				handler.sendMessage(mes);
			}
		});

		return v;
	}
}
